<template>
  <div>
    <div class="crumbs">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>
          <i class="el-icon-lx-goods"></i> 商城商品
        </el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div class="container">
      <div class="handle-box">
        <el-input v-model="search" placeholder="商品名称" class="handle-input mr10" width="55"></el-input>
        <el-button type="primary" icon="el-icon-search" @click="handleSearch">搜索</el-button>
        <el-button type="primary" icon="el-icon-lx-add" @click="showBox()">添加商品</el-button>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header" :row-style="{ height: '80px' }">
        <el-table-column label="地址" align="center">
          <template #default="scope">
            <div>{{ scope.row.province }}{{ scope.row.city }}{{ scope.row.district }}</div>
          </template>
        </el-table-column>
        <el-table-column label="详细地址" align="center">
          <template #default="scope">
            <div>{{ scope.row.address }}</div>
          </template>
        </el-table-column>
        <el-table-column prop="cq" label="产权"></el-table-column>
        <el-table-column prop="cx" label="朝向"></el-table-column>
        <el-table-column prop="flx" label="房源类型"></el-table-column>
        <el-table-column prop="fn" label="房龄(年)"></el-table-column>
        <el-table-column label="楼层" align="center">
          <template #default="scope">
            <el-tag>{{ scope.row.isDt ? '有电梯' : '无电梯' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="楼层" align="center">
          <template #default="scope">
            <div>第{{ scope.row.lc1 }}层/共{{ scope.row.lc2 }}层</div>
          </template>
        </el-table-column>
        <el-table-column prop="lxr" label="联系人"></el-table-column>
        <el-table-column prop="mobile" label="联系电话"></el-table-column>
        <el-table-column prop="ms" label="面积 m²"></el-table-column>
        <el-table-column prop="name" label="房产名称"></el-table-column>
        <el-table-column label="类型" align="center">
          <template #default="scope">
            <el-tag>{{ scope.row.type == 1 ? '租赁' : '出售' }}</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="xm" label="小区名称"></el-table-column>
        <el-table-column prop="zx" label="装修"></el-table-column>
        <el-table-column label="操作" width="250" align="center" v-if="username == 'admin'">
          <template #default="scope">
            <el-button type="text" class="green" @click="handleCheak(scope.row)">查看</el-button>
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleState(scope.row)">{{ scope.row.status == 0 ? "下架" : "上架" }}</el-button>
            <el-button type="text" icon="el-icon-delete" class="red" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" align="center" v-if="username == 'order'">
          <template #default="scope">
            <el-button type="text" class="green" @click="handleCheak(scope.row)">查看</el-button>
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button type="text" @click="handleState(scope.row)">{{ scope.row.status == 0 ? "下架" : "上架" }}</el-button>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="250" align="center" v-if="username == 'meigong'">
          <template #default="scope">
            <el-button type="text" class="green" @click="handleCheak(scope.row)">查看</el-button>
            <el-button type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">编辑</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex" :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>

    <!-- 编辑弹出框 -->
    <el-dialog title="创建" v-model="showAddUser" width="70%">
      <el-form ref="form" :model="list" label-width="100px" :disabled="isDisabled">
        <el-row>
          <el-col :span="8">
            <el-form-item label="详细地址">
              <el-input v-model="list.address"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="产权">
              <el-input v-model="list.cq"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="朝向">
              <el-select v-model="list.cx" placeholder="请选择">
                <el-option v-for="item in cxList" :key="item.value" :label="item.value" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房源类型">
              <el-select v-model="list.flx" placeholder="请选择">
                <el-option v-for="item in flxList" :key="item.value" :label="item.value" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房源类型">
              <el-select v-model="list.zx" placeholder="请选择">
                <el-option v-for="item in zxList" :key="item.value" :label="item.value" :value="item.value">
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="楼层"> <el-input v-model="list.lc1"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="总楼层"> <el-input v-model="list.lc2"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系人"> <el-input v-model="list.lxr"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="联系电话"> <el-input v-model="list.mobile"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="面积"> <el-input v-model="list.ms"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房产名称"> <el-input v-model="list.name"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="价格"> <el-input v-model="list.price"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="房屋亮点"> <el-input v-model="list.td"></el-input> </el-form-item>
          </el-col>
          <el-col :span="16">
            <el-form-item label="房型">
              <div style="display: flex;">
                <el-input v-model="list.ty1"></el-input>
                <div style="margin: 0 10px;">室</div>
                <el-input v-model="list.ty2"></el-input>
                <div style="margin: 0 10px;">厅</div>
                <el-input v-model="list.ty3"></el-input>
                <div style="margin: 0 10px;">卫</div>
              </div>
            </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="小区名称"> <el-input v-model="list.xm"></el-input> </el-form-item>
          </el-col>
          <el-col :span="8">
            <el-form-item label="是否有电梯">
              <el-radio-group v-model="list.isDt">
                <el-radio :label="true">有电梯</el-radio>
                <el-radio :label="false">无电梯</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="16">
            <el-form-item label="展示图片">
              <el-upload action="" list-type="picture-card" :http-request="uploadFileMethod" :on-preview="picPreview" multiple :file-list="picList" :on-remove="picRemove" :class="{ hide: hideUploadCard }" ref="picClear">
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog v-model="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-form-item>
          </el-col>
        </el-row>
        <el-form-item label="详情">
          <Tinymce-editor :disabled="isDisabled" :value="list.contextDetails" @input="(res) => (list.contextDetails = res)"></Tinymce-editor>
        </el-form-item>
        <el-form-item label="说明">
          <el-input type="textarea" placeholder="请输入内容" v-model="list.explains" show-word-limit :rows="12" resize="none">
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAddUser = false">取 消</el-button>
          <el-button v-if="isChange == 1" type="primary" @click="append">确 定</el-button>
          <el-button v-else type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import { upload, api } from "../../api/index";
import { formatDate } from "../../common/data";
import TinymceEditor from "../../components/tinymce-editor";
import moment from "moment";
export default {
  inject: ["reload"],
  filters: {
    formatDate(time) {
      time = time * 1000;
      let date = new Date(time);
      console.log(new Date(time));
      return formatDate(date, "yyyy-MM-dd hh:mm");
    },
  },
  components: {
    TinymceEditor,
  },
  name: "basetable",
  data() {
    return {
      username: localStorage.getItem('ms_username'),
      query: {
        pageNo: 1,
      },
      search: "",
      tableData: [],
      showAddUser: false,
      pageTotal: 0,
      list: {},
      classSonList: {},
      classSonListTwo: {},
      isChange: "",
      fileList: [],
      picList: [],
      isDisabled: false,
      downList: [],
      dialogVisible: false,
      hideUploadCard: false,
      cxList: [
        { value: '东', text: "东" },
        { value: "南", text: "南" },
        { value: "西", text: "西" },
        { value: "北", text: "北" },
        { value: "东西", text: "东西" },
        { value: "南北", text: "南北" },
        { value: "东南", text: "东南" },
        { value: "西南", text: "西南" },
        { value: "东北", text: "东北" },
        { value: "西北", text: "西北" },
      ],
      zxList: [
        { value: "毛坯", text: "毛坯" },
        { value: "简装", text: "简装" },
        { value: "精装", text: "精装" },
        { value: "豪装", text: "豪装" },
      ],
      flxList: [
        { value: "住宅", text: "住宅" },
        { value: "公寓", text: "公寓" },
        { value: "别墅", text: "别墅" },
        { value: "平房", text: "平房" },
        { value: "商住楼", text: "商住楼" },
        { value: "排屋", text: "排屋" },
      ],
    };
  },
  created() {
    this.getData();
  },
  mounted() { },
  methods: {
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    //创建分类
    append() {
      this.list.showDetails1 = this.fileList[0];
      this.list.showDetails2 = this.fileList[1];
      this.list.showDetails3 = this.fileList[2];
      this.list.commodityClassifyLevel2 = 0;
      api("/web/house-commodity/a", this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success("新增商品成功");
          this.list = {};
          this.reload();
          this.showAddUser = false;
        } else {
          this.$message.error(res.message);
        }
      });
    },
    joinpic(item) {
      return `http://xiuhaoshenghuo.xsiuyang.com/imgs/${item}`
    },
    ischange(e) {
      classSon({
        classifyPid: e,
      }).then((res) => {
        this.classSonListTwo = res.data;
      });
    },
    //预览大图
    picPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //移除图片
    picRemove(file, fileList) {
      this.fileList = [];
      for (const i in fileList) {
        this.fileList.push(fileList[i].url);
        if (fileList[i].raw) {
          this.uploadFileMethod(fileList[i].raw);
        }
      }
    },
    //多图上传
    uploadFileMethod(param) {
      let formData = new FormData();
      if (param.file) {
        formData.append("file", param.file);
      } else {
        formData.append("file", param);
      }
      upload(formData).then((res) => {
        this.fileList.push(this.joinpic(res.data[0].fileName));
        console.log(this.fileList);
        this.hideUploadCard = this.fileList.length >= this.isType;
      });
    },
    //上传图片
    setImage(file) {
      let imgFile = file.target.files[0];
      let formData = new FormData(); //通过formdata上传
      formData.append("file", imgFile);
      upload(formData).then((res) => {
        this.list.showPic = res.data[0].fileName;
      });
    },
    // 获取分类列表
    getData() {
      api("/web/house-commodity/g", {
        commodityClassifyLevel1: 0,
        limit: 10,
        offset: this.query.pageNo,
        name: this.search,
      }).then((res) => {
        // console.log(res.data);
        this.tableData = res.data.rows;
        this.pageTotal = res.data.total;
      });
    },
    //获取父级分类列表
    getclassSon() {
      classSon({
        classifyPid: 0,
      }).then((res) => {
        this.classSonList = res.data;
      });
    },
    //显示编辑框
    showBox() {
      this.showAddUser = true;
      this.isChange = 1;
      this.isDisabled = false;
    },
    // 触发搜索按钮
    handleSearch() {
      this.getData();
    },
    // 删除操作
    handleDelete(row) {
      // 二次确认删除
      let data = JSON.parse(JSON.stringify(row));
      this.$confirm("确定要删除吗？", "提示", {
        type: "warning",
      }).then(() => {
        api("/web/house-commodity/d", {
          id: data.id,
        }).then((res) => {
          if (res.code === 2000) {
            this.$message.success(res.message);
            this.reload();
          } else {
            this.$message.error(res.message);
          }
        });
      }).catch(() => { });
    },
    //查看操作
    handleCheak(row) {
      this.picList = [];
      this.isDisabled = true;
      this.showAddUser = true;
      this.isChange = 0;
      let data = JSON.parse(JSON.stringify(row));
      this.picList.push({ name: "1", url: data.showDetails1 });
      this.picList.push({ name: "2", url: data.showDetails2 });
      this.picList.push({ name: "3", url: data.showDetails3 });
      this.list = data;
    },
    // 编辑操作
    handleEdit(row) {
      this.picList = [];
      let data = JSON.parse(JSON.stringify(row));
      this.list = data;
      this.isDisabled = false;
      this.showAddUser = true;
      this.isChange = 0;
      this.picList.push({ name: "1", url: data.showDetails1 });
      this.picList.push({ name: "2", url: data.showDetails2 });
      this.picList.push({ name: "3", url: data.showDetails3 });
      // console.log(this.picList);
    },
    //修改分类
    edit() {
      if (this.fileList.length == 0) {
        for (const i in this.picList) {
          this.fileList.push(this.picList[i].url);
        }
      }
      // console.log(this.fileList);
      this.list.showDetails1 = this.fileList[0];
      this.list.showDetails2 = this.fileList[1];
      this.list.showDetails3 = this.fileList[2];
      this.list.commodityClassifyLevel2 = 0;
      this.showAddUser = false;
      api("/web/house-commodity/u", this.list).then((res) => {
        if (res.code === 2000) {
          this.$message.success("修改商品成功");
          this.list = {};
          this.reload();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    //上下架
    handleState(row) {
      // 二次确认删除
      let data = JSON.parse(JSON.stringify(row));
      // console.log(data.status);
      this.$confirm("确定改变状态", "提示", {
        type: "warning",
      }).then(() => {
        api("/web/house-commodity/up", {
          id: data.id,
          status: data.status == 0 ? "1" : "0",
        }).then((res) => {
          if (res.code === 2000) {
            this.$message.success(res.message);
            this.reload();
          } else {
            this.$message.error(res.message);
          }
        });
      })
    },

    // 分页导航
    handlePageChange(val) {
      // console.log(val);
      this.query.pageNo = val;
      this.getData();
    },
  },
};
</script>
<style>
.hide .el-upload--picture-card {
  display: none;
}
</style>
<style lang='scss' scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.green {
  color: #0ab618;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  height: 60px;
}

.crop-demo {
  display: flex;
  align-items: flex-end;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  width: 148px;
  height: 148px;

  .pre-img {
    width: 146px;
    height: 146px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
    object-fit: cover;
  }

  .crop-input {
    position: absolute;
    width: 148px;
    height: 148px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 148px;
    height: 148px;
    line-height: 148px;
    text-align: center;
  }
}
</style>
